package gui.funcionarios;

import ge.escola.Disciplina;
import ge.util.Horario;
import ge.util.Validadores;
import gui.GerenciadorDeJanelas;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class HorarioPanel extends javax.swing.JPanel {
	private TextArea mostraHorarios;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JButton removeHorario;
	private JButton voltarBotao;
	private JButton botaoAddHorario;
	private JLabel c1;
	private JComboBox disciplinasCombo;
	private JLabel jLabel7;
	private JLabel jLabel6;
	private JTextField minutos;
	private JLabel jLabel8;
	private JTextField hora;
	private JComboBox dias;
	private String horaInicial;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/

	
	public HorarioPanel() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(800, 600));
			this.setSize(800, 600);
			this.setLayout(null);
			this.setBackground(new java.awt.Color(204,204,255));
			{
				mostraHorarios = new TextArea();
				this.add(mostraHorarios);
				mostraHorarios.setText("jTextArea1");
				mostraHorarios.setBounds(61, 137, 222, 274);
				mostraHorarios.setEditable(false);
				atualizaBox();
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Horario do Professor");
				jLabel1.setBounds(213, 18, 344, 59);
				jLabel1.setFont(new java.awt.Font("Arial",0,36));
				jLabel1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				jLabel1.setForeground(new java.awt.Color(0,102,102));
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText("Horarios que estao ocupados:");
				jLabel2.setBounds(73, 109, 223, 16);
				jLabel2.setForeground(new java.awt.Color(0,102,102));
				jLabel2.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				ComboBoxModel diasModel = 
					new DefaultComboBoxModel(
							new String[] { "- - - - - - - -", "Segunda", "Terca", "Quarta", "Quinta", "Sexta", "Sabado", "Domingo"});
				dias = new JComboBox();
				this.add(dias);
				dias.setModel(diasModel);
				dias.setBounds(598, 155, 97, 23);
				
				
				
				
			}
			{
				jLabel3 = new JLabel();
				this.add(jLabel3);
				jLabel3.setText("Montar um Horario: ");
				jLabel3.setBounds(574, 109, 153, 16);
				jLabel3.setForeground(new java.awt.Color(0,102,102));
				jLabel3.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				jLabel4 = new JLabel();
				this.add(jLabel4);
				jLabel4.setText("Dia:");
				jLabel4.setBounds(562, 158, 24, 16);
				jLabel4.setForeground(new java.awt.Color(0,102,102));
				jLabel4.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				jLabel5 = new JLabel();
				this.add(jLabel5);
				jLabel5.setText("Hora inicial:");
				jLabel5.setBounds(510, 204, 76, 16);
				jLabel5.setForeground(new java.awt.Color(0,102,102));
				jLabel5.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				hora = new JTextField();
				this.add(hora);
				hora.setBounds(598, 201, 36, 23);
				hora.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						String horaI = hora.getText() + ":" + minutos.getText();
						if(!Validadores.validaHora(horaI)){
							c1.setForeground(Color.RED);
							c1.setText("INVALIDO");
						}else{
							c1.setForeground(Color.GREEN);
							c1.setText("OK");
							horaInicial = horaI;
							
						}
						
						
					}
				});
			}
			{
				minutos = new JTextField();
				this.add(minutos);
				minutos.setBounds(661, 201, 34, 23);
				minutos.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						String horaI = hora.getText() + ":" + minutos.getText();
						if(!Validadores.validaHora(horaI)){
							c1.setForeground(Color.RED);
							c1.setText("INVALIDO");
						}else{
							c1.setForeground(Color.GREEN);
							c1.setText("OK");
							horaInicial = horaI;
							
						}
					}
				});
			}
			{
				jLabel6 = new JLabel();
				this.add(jLabel6);
				jLabel6.setText("H");
				jLabel6.setBounds(633, 204, 10, 16);
				jLabel6.setForeground(new java.awt.Color(0,102,102));
				jLabel6.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				jLabel7 = new JLabel();
				this.add(jLabel7);
				jLabel7.setText("M");
				jLabel7.setBounds(695, 204, 11, 16);
				jLabel7.setForeground(new java.awt.Color(0,102,102));
				jLabel7.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				jLabel8 = new JLabel();
				this.add(jLabel8);
				jLabel8.setText("Disciplina:");
				jLabel8.setBounds(528, 254, 58, 16);
				jLabel8.setForeground(new java.awt.Color(0,102,102));
				jLabel8.setFont(new java.awt.Font("Arial",0,12));
			}
			{
				ComboBoxModel disciplinasModel = 
					new DefaultComboBoxModel(
							new String[] { "- - - - - - - -" });
				disciplinasCombo = new JComboBox();
				this.add(disciplinasCombo);
				disciplinasCombo.setModel(disciplinasModel);
				disciplinasCombo.setBounds(598, 251, 113, 23);
				atualizaBoxDisciplinas();
			}
			{
				c1 = new JLabel();
				this.add(c1);
				c1.setBounds(718, 204, 70, 16);
			}
			{
				botaoAddHorario = new JButton();
				this.add(botaoAddHorario);
				botaoAddHorario.setText("Adiciona Horario Montado");
				botaoAddHorario.setBounds(540, 319, 190, 23);
				botaoAddHorario.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				botaoAddHorario.setBackground(new java.awt.Color(255,255,255));
				botaoAddHorario.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						Disciplina disciplina = null;
						String dia;
						String disciplinaString = disciplinasCombo.getSelectedItem().toString();
						for(int i = 0; i < SelecionaFuncionario.professor.getListaDeDisciplinas().size(); i++){
							if(disciplinaString.equals(SelecionaFuncionario.professor.getListaDeDisciplinas().get(i).toString())){
								disciplina = SelecionaFuncionario.professor.getListaDeDisciplinas().get(i);
							}
						}if(disciplinaString.equals("Criar sem Disciplina")){
							disciplina = null;
						}
						//----------------------------------------------------------------
							dia = dias.getSelectedItem().toString();
						//----------------------------------------------------------------
						try {
							Horario horario = new Horario(dia, horaInicial, GerenciadorDeJanelas.escola.getDuracaoAula(), disciplina);
							SelecionaFuncionario.professor.addHorario(horario);
							JOptionPane.showMessageDialog(null, "Horario adicionado com sucesso");
							atualizaBox();
						} catch (Exception e) {
							JOptionPane.showMessageDialog(null, e.getMessage());
							
						}
					}
				});
			}
			{
				voltarBotao = new JButton();
				this.add(voltarBotao);
				voltarBotao.setText("Voltar");
				voltarBotao.setBounds(634, 512, 90, 32);
				voltarBotao.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						DisciplinasEHorarios discPanel = new DisciplinasEHorarios();
						GerenciadorDeJanelas.window.setContentPane(discPanel);
						GerenciadorDeJanelas.window.setVisible(true);
					}
				});
			}
			{
				removeHorario = new JButton();
				this.add(removeHorario);
				removeHorario.setText("Remove Horario Montado");
				removeHorario.setBounds(540, 358, 190, 22);
				removeHorario.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				removeHorario.setBackground(new java.awt.Color(255,255,255));
				removeHorario.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						Disciplina disciplina = null;
						String dia;
						String disciplinaString = disciplinasCombo.getSelectedItem().toString();
						for(int i = 0; i < SelecionaFuncionario.professor.getListaDeDisciplinas().size(); i++){
							if(disciplinaString.equals(SelecionaFuncionario.professor.getListaDeDisciplinas().get(i).toString())){
								disciplina = SelecionaFuncionario.professor.getListaDeDisciplinas().get(i);
							}
						}if(disciplinaString.equals("Criar sem Disciplina")){
							disciplina = null;
						}
						//----------------------------------------------------------------
							dia = dias.getSelectedItem().toString();
						//----------------------------------------------------------------
						try {
							Horario horario = new Horario(dia, horaInicial, GerenciadorDeJanelas.escola.getDuracaoAula(), disciplina);
							SelecionaFuncionario.professor.removeHorarioOcupado(horario);
							JOptionPane.showMessageDialog(null, "Horario removido com sucesso");
							atualizaBox();
						} catch (Exception e) {
							JOptionPane.showMessageDialog(null, e.getMessage());
							
						}
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	private void atualizaBoxDisciplinas() {
		for(int i = 0; i < SelecionaFuncionario.professor.getListaDeDisciplinas().size(); i++){
			disciplinasCombo.addItem(SelecionaFuncionario.professor.getListaDeDisciplinas().toString());
		}
		disciplinasCombo.addItem("Criar sem Disciplina");
		
	}

	public void atualizaBox(){
		String saida1 = "";
		try{
		for(int i = 0; i < SelecionaFuncionario.professor.getListaDeHorariosOcupados().size();i++){
			saida1 += SelecionaFuncionario.professor.getListaDeHorariosOcupados().get(i).toString();
		}
		}catch(Exception e){
			
		}
		mostraHorarios.setEditable(true);
		mostraHorarios.setText(saida1);
		mostraHorarios.setEditable(false);
	}

}
